package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.qf.ssm.ex.ServiceException;
import com.qf.ssm.mapper.CarManagementMapper;
import com.qf.ssm.mapper.CarRentMapper;
import com.qf.ssm.po.CarInfo;
import com.qf.ssm.po.CarRent;
import com.qf.ssm.po.CustomRent;
import com.qf.ssm.qo.CarInfoQo;
import com.qf.ssm.qo.CarRentQo;
import com.qf.ssm.qo.CarTypeQo;
import com.qf.ssm.qo.CustomRentQo;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CarManagementService;
import com.qf.ssm.utlis.CommonBeanutils;
import com.qf.ssm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.IntStream;


@Service
public class CarManagementServiceImpl implements CarManagementService {
    @Autowired
    private CarManagementMapper carManagementMapper;
    @Autowired
    private CarRentMapper carRentMapper;
    @Override
    public Integer saveCarInfo(CarInfoQo carInfoQo) {
        return carManagementMapper.insertCarInfo(CommonBeanutils.convertTo(carInfoQo, CarInfo::new));
    }

    @Override
    public PageInfo<CarInfoVo> carInfoList(int page, int size,  CarTypeQo carTypeQo) {
        PageInfo<CarInfoVo> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() ->
                carManagementMapper.selectCarInfoList(carTypeQo));
        CommonBeanutils.convertPageInfo(pageInfo,
                    new PageInfo<>(), CarInfoVo::new);
        if (ObjectUtils.isEmpty(pageInfo.getList())){
            throw new ServiceException(ResultStatus.INFO_NO_EXIST);
        }
    return pageInfo;
    }

    @Override
    public Integer modifyCarInfo(CarInfoQo carInfoQo, Integer cid) {
        int count = carManagementMapper.updateById(CommonBeanutils.convertTo(carInfoQo, CarInfo::new), cid);
        if (count<1){
            throw new ServiceException(ResultStatus.SQL_UPDATE_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeCarInfo(Integer cid) {
        int count = carManagementMapper.deleteById(cid);
        if (count<1){
            throw new ServiceException(ResultStatus.SQL_DELETE_ERROR);
        }
        return count;

    }

    @Override
    public Integer modifyStatus(Integer cid, Integer status) {
        return  carManagementMapper.updateStatusById(cid,status);
    }

    @Override
    public PageInfo<CarBrandVo> brandList(int page, int size) {
        PageInfo<CarBrandVo> carBrandVoPageInfo = CommonBeanutils.convertPageInfo(PageHelper.startPage(page, size).doSelectPageInfo(() -> carManagementMapper.selectBrandList()),
                new PageInfo<>(), CarBrandVo::new);
        if (ObjectUtils.isEmpty(carBrandVoPageInfo.getList())){
            throw new ServiceException(ResultStatus.BRAND_NO_EXIST);
        }
        return carBrandVoPageInfo;
    }

    @Override
    public CarInfoVo queryCarInfo(Integer cid) {
        CarInfo carInfo = carManagementMapper.selectCarInfoById(cid);
        CarInfoVo carInfoVo = CommonBeanutils.convertTo(carInfo, CarInfoVo::new);
        carInfoVo.setCarTypeVo(CommonBeanutils.convertTo(carInfo.getCarType(), CarTypeVo::new));
        if (ObjectUtils.isEmpty(carInfo)){
            throw new ServiceException(ResultStatus.INFO_NO_EXIST);
        }
        return carInfoVo;
    }


    @Transactional
    @Override
    public Integer saveOrModifyCarRent(Integer cid, CarRentQo carRentQo) {
        CarRent carRent = carRentMapper.selectCarRentById(cid);
            if (ObjectUtils.isEmpty(carRent)){
            CarRent carRentNew=CommonBeanutils.convertTo(carRentQo,CarRent::new);
            carRentNew.setCustomRentList(CommonBeanutils.convertListTo(carRentQo.getCustomRentQoList(), CustomRent::new));
                 int count = carRentMapper.insertCarRentById(carRentNew, cid);
                if (count<1){
                    throw new ServiceException(ResultStatus.RANT_ADD_ERROR);
                }
                carRentQo.getCustomRentQoList().stream().forEach(( CustomRentQo customRentQo) ->{
                int count1=carRentMapper.insertCustomRentByRId(CommonBeanutils.convertTo(customRentQo,CustomRent::new),carRentNew.getRid());
                if (count1<1){
                    throw new ServiceException(ResultStatus.CustomRent_ADD_ERROR);
                }
            });
            return 1;
        }else
        {
            CarRent CarRentNew = CommonBeanutils.convertTo(carRentQo, CarRent::new);
            int count = carRentMapper.updateCarRentById(CarRentNew, cid);
            if (count<1){
                throw new ServiceException(ResultStatus.RANT_UPDATE_ERROR);
            }
            carRentQo.getCustomRentQoList().stream().forEach(( CustomRentQo customRentQo) ->{
                if (customRentQo.getCrId()!=0){
                    int count1 = carRentMapper.updateCustomRentByCrId(CommonBeanutils.convertTo(customRentQo, CustomRent::new), customRentQo.getCrId());
                    if (count<1){
                        throw  new ServiceException(ResultStatus.CustomRent_UPDATE_ERROR);
                    }
                }else {
                    int count1=carRentMapper.insertCustomRentByRId(CommonBeanutils.convertTo(customRentQo,CustomRent::new),carRent.getRid());
                    if (count<1){
                        throw new ServiceException(ResultStatus.CustomRent_ADD_ERROR);
                }
            }});
        }
        return 1;
    }

    @Override
    public CarRentVo queryCarRent(Integer cid) {
        CarRent carRent = carRentMapper.selectCarRentById(cid);
        CarRentVo carRentVo = CommonBeanutils.convertTo(carRent, CarRentVo::new);
        carRentVo.setCustomRentQoList(CommonBeanutils.convertListTo(carRent.getCustomRentList(), CustomRentVo::new));
        return carRentVo;
    }


}
